﻿using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;

namespace WzhModule.GM
{
    //[Beebyte.Obfuscator.Skip]
    public class WzhGMContent : MonoBehaviour
    {
        public static WzhGMContent I { get; private set; }

        private List<WzhGMMenu> GMMenuList = new List<WzhGMMenu>();

        private List<WzhGMMenu> GlobalGMMenuList = new List<WzhGMMenu>();

        public WzhGMPanel GMPanel = null;

        /// <summary>小窗口panel</summary>
        [SerializeField] public GameObject MiniPanel;
        /// <summary>折叠窗窗口</summary>
        [SerializeField] protected GameObject DropDownPanel;
        /// <summary>输入框</summary>
        [SerializeField] public InputField textInputField;
        /// <summary>Mini输入框</summary>
        [SerializeField] public InputField textMiniInputField;


        [SerializeField] protected Button MaxWinButton;

        [SerializeField] protected Button DropDownButton;

        [SerializeField] protected GameObject SidingPanel;
        [SerializeField] public Button btnClose;
        [SerializeField] public Button btnMinimize;

        public float CanvasWidth => WzhGMControl.CanvasWidth;
        public float CanvasHeight => WzhGMControl.CanvasHeight;

        public void Awake()
        {
            I = this;
            if (WzhGMControl.assemblyList != null)
                foreach (var item in WzhGMControl.assemblyList)
                    AddDebugMenu(item);


            MaxWinButton.onClick.AddListener(OnClickDBGButton);

            DropDownButton.onClick.AddListener(OnClickDropDown);

            btnClose.onClick.AddListener(OnClickClose);
            btnMinimize.onClick.AddListener(OnClickMinimize);

            textInputField.onValueChanged.AddListener(delegate { OnInputFieldValueChanged(textInputField, textMiniInputField); });
            textMiniInputField.onValueChanged.AddListener(delegate { OnInputFieldValueChanged(textMiniInputField, textInputField); });

            try
            {
                foreach (WzhGMMenu settings in GMMenuList)
                {
                    settings.Init();
                    GlobalGMMenuList.Add(settings);
                    settings.GMContent = this;
                    settings.GMPanel = GMPanel;
                }
                GMPanel.Repopulate(GlobalGMMenuList);

            }
            catch (Exception e)
            {
                WzhGMControl.I.Error(e.ToString());
            }
            GMPanel.gameObject.SetActive(false);
            SwitchSidingDebug(false);
            AdaptationInterfaceFor540x960();
        }

        /// <summary>Canvas Scaler为540*960的GM界面适配</summary>
        private void AdaptationInterfaceFor540x960()
        {
            //只有Canvas Scaler为540*960才进行调整
            if (CanvasWidth != 540.0f || CanvasHeight != 960.0f) return;

            GameObject GMRoot = transform.parent.gameObject;
            RectTransform GMRect = GMRoot.GetComponent<RectTransform>();

            //设置GM的适配
            GMRect.anchorMax = Vector3.one / 2.0f;
            GMRect.anchorMin = Vector3.one / 2.0f;
            GMRect.sizeDelta = new Vector2(1080.0f, 1920.0f);

            //获取缩放倍数，取1080*1920的长宽倍数的平均值
            float scaleRate = ((CanvasWidth / 1080.0f) + (CanvasHeight / 1920.0f)) / 2.0f;

            GMRect.localScale = Vector3.one * scaleRate;
        }

        private void OnInputFieldValueChanged(InputField inputField, InputField otherInputField)
        {
            otherInputField.text = inputField.text;
        }

        void AddDebugMenu(Assembly assembly)
        {
            Type[] exportedTypes = assembly.GetExportedTypes();
            Type[] array = exportedTypes;
            foreach (Type type in array)
            {
                WzhGMClass customAttribute = type.GetCustomAttribute<WzhGMClass>();
                if (customAttribute != null)
                {
                    if (IgnoreType(type)) continue;

                    if (customAttribute.IsLeftPanel)
                    {
                        var leftDebugMenu = new LeftDebugMenu();
                        GMPanel.LeftDebuggableObject = leftDebugMenu;
                        leftDebugMenu.Sequence = customAttribute.Sequence;
                        GMMenuList.Add(leftDebugMenu);
                    }
                    else if (customAttribute.IsRightPanel)
                    {
                        var rightDebugMenu = new RightDebugMenu();
                        GMPanel.RightDebuggableObject = rightDebugMenu;
                        rightDebugMenu.Sequence = customAttribute.Sequence;
                        GMMenuList.Add(rightDebugMenu);
                    }
                    else
                    {
                        WzhGMMenu debugMenu = (WzhGMMenu)Activator.CreateInstance(type);
                        debugMenu.Sequence = customAttribute.Sequence;
                        GMMenuList.Add(debugMenu);
                    }


                }
            }
        }

        /// <summary>获得debug 分页</summary>
        public T GetDebugMenu<T>() where T : WzhGMMenu
        {
            foreach (var item in GMMenuList)
                if (item is T) return item as T;
            return null;
        }
        /// <summary>忽略的类型</summary>
        private bool IgnoreType(Type t)
        {
            List<Type> IgnoreTypes = new List<Type>()
            {
                //#if UNITY_EDITOR
                //                typeof(OnlineDataDebugMenu),
                //                typeof(InfoDebugMenu),
                //#endif
            };
            return IgnoreTypes.Contains(t);
        }


        /// <summary>打开GM</summary>
        public void OnOpen()
        {
            MiniPanel.gameObject.SetActive(true);
            GMPanel.gameObject.SetActive(false);

        }
        private void Update()
        {
            for (int i = 0; i < GMMenuList.Count; i++)
                GMMenuList[i].OnUpdate();
        }

        /// <summary>显示面板</summary>
        public virtual void OnClickDBGButton()
        {
            MiniPanel.gameObject.SetActive(false);
            GMPanel.gameObject.SetActive(true);
            GMPanel.OpenGUIDebug();
        }

        /// <summary>折叠窗</summary>
        public virtual void OnClickDropDown()
        {
            if (DropDownPanel.activeSelf)
            {
                DropDownButton.transform.GetChild(1).gameObject.SetActive(true);
                DropDownPanel.SetActive(false);
                textMiniInputField.gameObject.SetActive(false);
            }
            else
            {
                DropDownPanel.SetActive(true);
                DropDownButton.transform.GetChild(1).gameObject.SetActive(false);
                textMiniInputField.gameObject.SetActive(true);

            }
        }

        /// <summary>点击关闭</summary>
        public void OnClickClose()
        {
            gameObject.SetActive(false);
        }

        /// <summary>点击最小化</summary>
        public void OnClickMinimize()
        {
            GMPanel.gameObject.SetActive(false);
            MiniPanel.gameObject.SetActive(true);
        }


        #region 两边GM

        /// <summary>显示面板</summary>
        public void SwitchSidingDebug(bool change = true)
        {
            //bool show = UserDataPrefs.GetInt("ShowingSidingDebug") == 1;
            //if (change) show = !show;
            //SidingPanel.SetActive(show);
            //if (show) DebugSettings.ShowLeftAndRightPanel();
            //UserDataPrefs.SetInt("ShowingSidingDebug", show ? 1 : 0);
        }
        [Obsolete("请用SwitchSidingDebug替换")]
        public void ShowSidingDebug(bool showGm)
        {
            SwitchSidingDebug();
        }
        /// <summary>隐藏二边的UI</summary>
        public void HideSidingDebug()
        {
            SidingPanel.SetActive(false);
        }
        #endregion

   

        private void OnDestroy()
        {
            foreach (var item in GMMenuList)
                item.OnDispose();
            I = null;
        }
    }

}
